package com.company.factory.simple;

import com.company.factory.simple.impl.Dazhong;
import com.company.factory.simple.impl.Tesla;
import com.company.factory.simple.impl.WuLin;
import com.sun.xml.internal.bind.v2.ClassFactory;

import java.io.*;
import java.util.Properties;

public class CarFactory {
    //方法一
    public static Car getCar(String car) {
        if (car.equals("五菱")) {
            return new WuLin();
        } else if (car.equals("特斯拉")) {
            return new Tesla();
        } else {
            return null;
        }
    }

    //方法二
    public static Car getWuling() {
        return new WuLin();
    }

    public static Car getTesla() {
        return new Tesla();
    }

    public static Car getDazhong() {
        return new Dazhong();
    }

    //方法三
    public static Car getCarByBrand(String carBrand) {
        InputStream inputStream = CarFactory.class.getResourceAsStream("application.properties");
        {
            try {
                Properties properties = new Properties();
                properties.load(inputStream);
                return (Car) Class.forName(properties.getProperty(carBrand)).newInstance();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
